package com.cc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.comm.responce.PageResult;
import com.cc.model.dto.EmployeeQuery;
import com.cc.model.entity.Employees;
import com.cc.mapper.EmployeesMapper;
import com.cc.service.EmployeesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 员工登录表 服务实现类
 * </p>
 *
 * @author 晓东√
 * @since 2025-09-19 11:18
 */
@Service
public class EmployeesServiceImpl extends ServiceImpl<EmployeesMapper, Employees>
        implements EmployeesService {
    @Autowired
    private EmployeesMapper employeesMapper;
    @Override
    public PageResult<Employees> getEmployeeList(EmployeeQuery employeeQuery) {
        // 分页查询
        Page<Employees> page = new Page<>(employeeQuery.getPageNum(), employeeQuery.getPageSize());
        // 查询条件
        LambdaQueryWrapper<Employees> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(employeeQuery.getUsername()),
                        Employees::getEmpName, employeeQuery.getUsername())
                .like(StringUtils.isNotBlank(employeeQuery.getPhone()),
                        Employees::getPhoneNumber, employeeQuery.getPhone());
        // 执行查询 返回mybatis的分页结果
        Page<Employees> employeesPage = employeesMapper.selectPage(page, queryWrapper);

        return PageResult.of(employeesPage);
    }

    @Override
    public void editEmployee(Employees employee) {
        if (employee == null) {
            return;
        }
        Integer employeeId = employee.getEmployeeId();
        if (employeeId == null) {
            employee.setPassword("123456");
            // 新增
            this.save(employee);
        } else {
            // 仅更新前端传递的非空字段
            this.lambdaUpdate()
                    .eq(Employees::getEmployeeId, employeeId)
                    .set(employee.getLoginName() != null, Employees::getLoginName, employee.getLoginName())
                    .set(employee.getEmpName() != null, Employees::getEmpName, employee.getEmpName())
                    .set(employee.getPosition() != null, Employees::getPosition, employee.getPosition())
                    .set(employee.getEmail() != null, Employees::getEmail, employee.getEmail())
                    .set(employee.getPhoneNumber() != null, Employees::getPhoneNumber, employee.getPhoneNumber())
                    .set(employee.getSalary() != null, Employees::getSalary, employee.getSalary())
                    .update();
        }
    }

    @Override
    public void resetPassword(Integer id) {
        this.lambdaUpdate()
                .eq(Employees::getEmployeeId, id)
                .set(Employees::getPassword, "123456")
                .update();
    }
}
